import WxValidate from '../../utils/WxValidate.js';
var interval = null;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        form: {
            phone: "",
            code: "",
            phoneError: "",
            codeError: ""
        },
        codeDisable: true,
        time: "获取验证码",
        currentTime: 59
    },
    phoneCheck(e) {
        this.setData({ "form.phoneError": '' });
        if (!this.WxValidate.methods.required(e.detail.value)) {
            this.setData({ "form.phoneError": '请输入手机号' });
            this.setData({ codeDisable: true });
            return;
        }
        if (!this.WxValidate.methods.tel(e.detail.value)) {
            this.setData({ "form.phoneError": '请输入正确的手机号' });
            this.setData({ codeDisable: true });
            return;
        }
        if(!interval){
            this.setData({ codeDisable: false });
        }
    },
    codeCheck(e) {
        this.setData({ "form.codeError": '' });
        if (!this.WxValidate.methods.required(e.detail.value)) {
            this.setData({ "form.codeError": '请输入验证码' });
            return;
        }
    },
    getCode() {
        this.setData({ codeDisable: true });
        var currentTime = this.data.currentTime
        this.setData({ time: '59秒后获取' });
        interval = setInterval(() => {
            currentTime--;
            this.setData({
                time: currentTime + '秒后获取'
            })
            if (currentTime <= 0) {
                clearInterval(interval)
                this.setData({
                    time: '获取验证码',
                    currentTime: 59,
                    codeDisable: false
                })
            }
        }, 1000)
    },
    login(e) {
        this.setData({ "form.phoneError": '' });
        this.setData({ "form.codeError": '' });
        const params = e.detail.value
        console.log(params)
        // 传入表单数据，调用验证方法
        if (!this.WxValidate.checkForm(params)) {
            const error = this.WxValidate.errorList;
            console.log(error)
            error.forEach(e => {
                console.log(e)
                switch (e.param) {
                    case "phone":
                        this.setData({ "form.phoneError": e.msg });
                        console.log("手机号错了", e.msg)
                        break;
                    case "code":
                        this.setData({ "form.codeError": e.msg });
                        break;
                }
            });
            console.log(this.data.form.phoneError)
            return false;
        }
        console.log("校验通过")
    },
    initValidate() {
        const rules = {
            phone: {
                required: true,
                tel: true,
            },
            code: {
                required: true,
            },
        }
        const messages = {
            phone: {
                required: '请输入手机号',
                tel: '请输入正确的手机号'
            },
            code: {
                required: '请输入验证码',
            },
        }
        // 创建实例对象
        this.WxValidate = new WxValidate(rules, messages);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.initValidate();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})